using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Guis : MonoBehaviour {
	
	public drawTiles dT;
	public info world;
	public DunGenAlgo theDun;
	public Texture2D greens;
	public Texture2D greys;
	public bool drawSplits = false;
	public bool drawRooms = false;
	public Texture2D yellow;
	public Texture2D purps;
	public Texture2D lgtGrey;
	public static int worldID = 0;
	public bool popUP = false;
	public Params thisPlace = new Params();
	public string FizeHere = "";
	public string yHere = "";
	public string ratioBox = "";
	public string numSplt = "";
	public bool on = true;
	
	// Use this for initialization
	void Start () {

	theDun = new DunGenAlgo();
		theDun.Start(thisPlace);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void OnGUI()
	{
		
	if(on)
		{
		
		#region
		if(Event.current.type.Equals(EventType.Repaint))
		{
		Graphics.DrawTexture(new Rect(175,0,thisPlace.dunSize.width,thisPlace.dunSize.height),greys);
		}
		
		//GUI.BeginGroup (new Rect (Screen.width-400, 10, 200, 300));
		
		GUI.Label(new Rect(0,280,100,25),"Dungeon Size");
		GUI.Label(new Rect(0,310,15,20),"X: ");
		FizeHere = GUI.TextField(new Rect(15,310,70,20),FizeHere,25);
		GUI.Label(new Rect(0,330,15,20),"Y: ");
		yHere = GUI.TextField(new Rect(15,330,70,20),yHere,25);
	
		
		if(GUI.Button(new Rect(100,280,50,20),"Set"))
		{
			if(FizeHere =="")
			{
			
			}
			else
			{
				thisPlace.dunSize.width = float.Parse(FizeHere);
				theDun.roomsforShow.Clear();
				theDun.hallways = new List<Rectangles>();
				theDun.hallways.Clear();
				theDun = new DunGenAlgo();
				theDun.Start(thisPlace);
			}
			if(yHere =="")
			{
				
			}
			else
			{
				thisPlace.dunSize.height = float.Parse(yHere);
				theDun.roomsforShow.Clear();
				theDun.hallways = new List<Rectangles>();
				theDun.hallways.Clear();
				theDun = new DunGenAlgo();
				theDun.Start(thisPlace);
			}
			
		
			
		}
		if(GUI.Button(new Rect(15,365,150,20),"Explore"))
		{
		if(theDun.hallways.Count>1)
				{
		 loadNewUse();
				}
		}
		
		//GUI.EndGroup();
		
		//GUI.BeginGroup (new Rect (Screen.width-200, 10, 200, 300));
		
		if(GUI.Button(new Rect(15,50,70,30),"Split"))
		   {
			drawRooms = false;
			drawSplits = true;
			theDun.Split(theDun.root);
			}
		
		
		
			
			
			if(GUI.Button(new Rect(15,185,70,35),"Reset"))
		{
			theDun.roomsforShow.Clear();
			theDun.hallways = new List<Rectangles>();
			theDun.hallways.Clear();
			theDun = new DunGenAlgo();
			thisPlace.dunSize.width = 600;
			thisPlace.dunSize.height = 600;
			thisPlace.ratio = (float)1.5;
			theDun.Start(thisPlace);
			
		}
		if(GUI.Button(new Rect(15,135,70,35),"Corridors"))
		{
			theDun.hallways.Clear();
			
			if (theDun.leafs.Count==0)
			{
				popUP = true;
				
			}
				if(!popUP)
			{
			theDun.generateCorridor(theDun.leafs);
			}
			drawRooms = true;
			drawSplits = false;
		}
		
		if(GUI.Button(new Rect(15,90,70,35),"Rooms"))
		{
			theDun.hallways.Clear();
			theDun.roomsforShow.Clear();
			theDun.leafs.Clear();
			theDun.generateRoom(theDun.root);
			drawRooms = true;
			drawSplits = false;
		}
		
		
		//GUI.EndGroup();
		
		
		
		
		
		
		
		if(drawSplits)
		{
			#region
		if(Event.current.type.Equals(EventType.Repaint))
		{
			for(int i = 0;i<theDun.listOfSplits.Count;i++)
			{
			if (theDun.listOfSplits[i].start.y==theDun.listOfSplits[i].end.y)
				{
					Graphics.DrawTexture(new Rect(theDun.listOfSplits[i].start.x,theDun.listOfSplits[i].start.y,theDun.listOfSplits[i].end.x,1),greens);
				}
				else
				{
				Graphics.DrawTexture(new Rect(theDun.listOfSplits[i].start.x,theDun.listOfSplits[i].start.y,1,theDun.listOfSplits[i].end.y),greens);	
				}
			}
		}
			#endregion
		}
		
		
				if(drawRooms)
		{
			#region
		if(Event.current.type.Equals(EventType.Repaint))
		{
			for(int i = 0;i<theDun.roomsforShow.Count;i++)
			{
			
				if(i<=(theDun.roomsforShow.Count/2)-1)
					{
				Graphics.DrawTexture(new Rect(theDun.roomsforShow[i].x,	theDun.roomsforShow[i].y,theDun.roomsforShow[i].width,theDun.roomsforShow[i].height),yellow);
				
					}
					else
					{
						Graphics.DrawTexture(new Rect(theDun.roomsforShow[i].x,	theDun.roomsforShow[i].y,theDun.roomsforShow[i].width,theDun.roomsforShow[i].height),greens);
				
						
					}
			}
		}
			#endregion
			
		if(Event.current.type.Equals(EventType.Repaint))
		{
			for(int i = 0;i<theDun.hallways.Count;i++)
			{
			
				
				Graphics.DrawTexture(new Rect(theDun.hallways[i].x,	theDun.hallways[i].y,theDun.hallways[i].width,theDun.hallways[i].height),purps);
			}
		}	
		}
		if(popUP)
		{
			Graphics.DrawTexture(new Rect(Screen.width/2-100,Screen.height/2-100,200,200),lgtGrey);
			GUI.Label(new Rect(Screen.width/2-98,Screen.height/2-70,200,50),"You need Rooms before you can do hallways");
			if(GUI.Button(new Rect(Screen.width/2-25,Screen.height/2+15,50,50),"OK"))
			   {
				popUP = false;
				}
			
		}
		#endregion
	
		}
		}
			
	public void loadNewUse()
	{
		world.fillGrid(1250,1250);
		world.putRooms(theDun.roomsforShow);
		world.putHallways(theDun.hallways);
				world.placePlayer();
				dT.on = true;
				on = false;
		
	}
	
	public void randoDun()
	{
		thisPlace.dunSize.height = 150;
		thisPlace.dunSize.width = 150;
		
		theDun.roomsforShow.Clear();
			theDun.hallways = new List<Rectangles>();
			theDun.hallways.Clear();
			theDun = new DunGenAlgo();
			theDun.Start(thisPlace);	
		for(int i = 0;i<5;i++)
				{
					theDun.Split(theDun.root);
				}
		theDun.generateRoom(theDun.root);
		theDun.generateCorridor(theDun.leafs);
		loadNewUse();
	}
	
}
